<script setup>
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import Connection from './pages/Connection.vue'
import Publish from './pages/Publish.vue'
import Subscribe from './pages/Subscribe.vue'
import Logs from './pages/Logs.vue'

const active = ref('connection') // 默认显示连接页面

// 移动端检测
const isMobile = ref(false)
const isPortrait = ref(false)

// 移动端导航标签
const mobileTabs = computed(() => [
  { name: 'connection', label: '连接', icon: 'el-icon-connection' },
  { name: 'publish', label: '发布', icon: 'el-icon-s-promotion' },
  { name: 'subscribe', label: '订阅', icon: 'el-icon-bell' },
  { name: 'logs', label: '日志', icon: 'el-icon-document' }
])

// 获取当前页面标题
const getCurrentPageTitle = () => {
  const titles = {
    connection: 'MQTT 连接',
    publish: '发布消息',
    subscribe: '订阅消息',
    logs: '日志查看'
  }
  return titles[active.value] || 'MQTT 控制台'
}

// 切换标签页，添加过渡效果和触摸反馈
const switchTab = (tabName) => {
  // 防止重复点击
  if (active.value === tabName) return
  
  // 正确更新ref值
  active.value = tabName
}

// 优化设备检测逻辑，确保在所有访问方式下都能正确识别
const checkDeviceStatus = () => {
  // 优先使用屏幕宽度检测，更加可靠
  const screenWidth = window.innerWidth;
  // 同时检查用户代理字符串作为补充
  const userAgent = navigator.userAgent.toLowerCase();
  const isMobileUA = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);
  
  // 综合判断：屏幕宽度小于等于768px或者用户代理显示为移动设备
  isMobile.value = screenWidth <= 768 || isMobileUA;
  isPortrait.value = window.innerHeight > window.innerWidth;
  
  // 调试信息（可移除）
  console.log('设备检测:', { screenWidth, isMobileUA, isMobile: isMobile.value });
}

onMounted(() => {
  checkDeviceStatus()
  window.addEventListener('resize', checkDeviceStatus)
  window.addEventListener('orientationchange', checkDeviceStatus)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', checkDeviceStatus)
  window.removeEventListener('orientationchange', checkDeviceStatus)
})
</script>

<template>
  <div class="app-container">
    <!-- 移动端头部 -->
    <div v-if="isMobile" class="mobile-header">
      <div class="header-content">
        <div class="app-title">{{ getCurrentPageTitle() }}</div>
      </div>
    </div>

    <!-- 桌面端头部 -->
    <div v-else class="desktop-header">
      <div class="header-content">
        <div class="app-title">MQTT 客户端控制台</div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 移动端内容区域 -->
      <div v-if="isMobile" class="mobile-content">
        <Connection v-if="active === 'connection'" />
        <Publish v-else-if="active === 'publish'" />
        <Subscribe v-else-if="active === 'subscribe'" />
        <Logs v-else-if="active === 'logs'" />
      </div>

      <!-- 桌面端标签页布局 -->
      <div v-else class="desktop-tabs">
        <el-tabs v-model="active" type="card">
          <el-tab-pane label="连接设置" name="connection">
            <Connection />
          </el-tab-pane>
          <el-tab-pane label="发布消息" name="publish">
            <Publish />
          </el-tab-pane>
          <el-tab-pane label="订阅消息" name="subscribe">
            <Subscribe />
          </el-tab-pane>
          <el-tab-pane label="日志查看" name="logs">
            <Logs />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>

    <!-- 移动端底部导航 -->
    <div v-if="isMobile" class="mobile-nav">
      <div 
        v-for="tab in mobileTabs" 
        :key="tab.name"
        class="nav-item"
        :class="{ active: active.value === tab.name }"
        @click="switchTab(tab.name)"
      >
        <i :class="tab.icon"></i>
        <span>{{ tab.label }}</span>
      </div>
    </div>
  </div>
</template>

<!-- 已将所有逻辑移至 script setup 中 -->

<style scoped>
/* CSS 变量定义 */
:root {
  --mobile-header-height: 56px;
  --mobile-nav-height: 60px;
  --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-inset-top: env(safe-area-inset-top, 0px);
}

.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
  -webkit-tap-highlight-color: transparent; /* 去除触摸高亮 */
}

/* 移动端头部样式 */
.mobile-header {
  height: var(--mobile-header-height);
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.app-title {
  font-size: 18px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 80%;
}

.header-actions {
  display: flex;
  gap: 8px;
}

/* 桌面端头部 */
.desktop-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 16px 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* 主要内容区域 */
.main-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  z-index: 1;
  /* 优化滚动性能 */
  will-change: transform;
  -ms-overflow-style: none;
  scrollbar-width: thin;
}

.main-content::-webkit-scrollbar {
  display: none; /* 隐藏滚动条但保留功能 */
}

/* 移动端内容区域 */
.mobile-content {
  padding: 16px;
  padding-bottom: calc(var(--mobile-nav-height) + 16px); /* 为底部导航留出空间 */
  /* 移除嵌套滚动，避免滚动冲突 */
  height: auto;
  min-height: 100%;
  overflow: visible;
  /* 改善触摸体验 */
  -webkit-touch-callout: none;
  user-select: none;
}

/* 桌面端标签页布局 */
.desktop-tabs {
  height: 100%;
  padding: 12px;
  overflow-y: auto;
}

/* 移动端底部导航样式 */
.mobile-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--mobile-nav-height) + var(--safe-area-inset-bottom));
  background-color: #ffffff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
  z-index: 10;
  padding-bottom: var(--safe-area-inset-bottom);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 8px 0;
  transition: all 0.2s ease;
}

.nav-item i {
  font-size: 20px;
  margin-bottom: 4px;
  color: #606266;
}

.nav-item span {
  font-size: 12px;
  color: #606266;
}

.nav-item.active i,
.nav-item.active span {
  color: #667eea;
}

/* 响应式断点 */
@media (max-width: 768px) {
  .app-title {
    font-size: 16px;
  }
  
  .mobile-content {
    padding: 12px;
    padding-bottom: calc(var(--mobile-nav-height) + 12px);
  }
}

@media (max-width: 480px) {
  .app-title {
    font-size: 16px;
  }
  
  .mobile-nav {
    --mobile-nav-height: 56px;
  }
  
  .nav-item i {
    font-size: 18px;
  }
  
  .nav-item span {
    font-size: 11px;
  }
}

/* 高DPI屏幕优化 */
@media (-webkit-device-pixel-ratio: 2), (resolution: 192dpi) {
  .mobile-nav {
    box-shadow: 0 -1px 5px rgba(0,0,0,0.05);
  }
}

/* 全局样式优化 */
:deep(.el-card) {
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  margin-bottom: 16px;
  overflow: hidden;
}

:deep(.el-button) {
  border-radius: 8px;
  transition: all 0.2s ease;
}

:deep(.el-input) {
  border-radius: 8px;
}

:deep(.el-input__inner) {
  border-radius: 8px;
  transition: all 0.2s ease;
}

:deep(.el-input__inner:focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

/* 移动端触摸优化 */
@media (hover: none) and (pointer: coarse) {
  .nav-item:active {
    background-color: rgba(0,0,0,0.05);
  }
}

/* 保持白色背景，移除深色模式适配 */

/* 减少动画和过渡效果以提高性能（适用于低性能设备） */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
</style>